﻿#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
#endregion

namespace ProcolServices
{
    /// <summary>
    /// Summary description for MyTeamsService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class MyTeamsService : WebService
    {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public void DelUserFromTeam(int userId, int teamId)
        {
            //Update in UserTeamHistoryValue
            var userTeam = (from i in db.UserTeamHistories
                            where i.UserId == userId && i.TeamId == teamId
                           select i).Single();

            userTeam.LeaveDate = DateTime.Now;

            db.SubmitChanges();

            //Del from UserTeam table
            var rowstoDelete =
                    from i in db.UserTeams
                    where i.UserId == userId && i.TeamId == teamId
                    select i;

            foreach (var row in rowstoDelete)
            {
                db.UserTeams.DeleteOnSubmit(row);
            }

            try
            {
                db.SubmitChanges();
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e);
                // Provide for exceptions.
            }
        }
        [WebMethod]
        public TeamBean[] GetTutorTeams(int tutorId)
        {
            var list = new List<TeamBean>();
            var query = from i in db.Teams
                        where i.TutorId == tutorId
                        select new
                        {
                            i.TeamId
                        };

            foreach (var q in query)
            {
                var query2 = from i in db.Teams
                             where i.TeamId == q.TeamId
                             select new
                             {
                                 i.ProjectId,
                                 i.TeamId,
                                 i.Name
                             };
                foreach (var q2 in query2)
                {
                    var query3 = from i in db.Projects
                                 where i.ProjectId == q2.ProjectId
                                 select new
                                 {
                                     i.Name
                                 };
                    var query4 = from i in db.UserTeams
                                 where i.TeamId == q2.TeamId
                                 select new
                                 {
                                     i.UserId
                                 };
                    var listMember = new List<MemberBean>();
                    foreach (var q4 in query4)
                    {
                        var query5 = from i in db.Users
                                     where i.UserId == q4.UserId
                                     select new
                                     {
                                         i.UserId,
                                         i.Username,
                                         i.Name,
                                         i.LastnameD,
                                         i.LastnameM,
                                         i.MajorId,
                                         i.Email
                                     };
                        listMember.Add(new MemberBean
                        {
                            userId = query5.First().UserId,
                            username = query5.First().Username,
                            name = query5.First().Name,
                            lastnameD = query5.First().LastnameD.Trim(),
                            lastnameM = query5.First().LastnameM.Trim(),
                            major = query5.First().MajorId.Trim(),
                            email = query5.First().Email
                        });
                    }

                    list.Add(new TeamBean
                    {
                        teamId = q2.TeamId,
                        projectId = q2.ProjectId,
                        teamName = q2.Name.Trim(),
                        projectName = query3.First().Name.Trim(),
                        memberList = listMember
                    });
                }


            }

            return list.ToArray();
        }

        [WebMethod]
        public TeamBean[] GetListOfMyTeams(int userId)
        {
            var list = new List<TeamBean>();
            var query = from i in db.UserTeams
                        where i.UserId == userId
                        select new
                        {
                            i.TeamId
                        };

            foreach (var q in query)
            {
                var query2 = from i in db.Teams
                            where i.TeamId == q.TeamId
                            select new
                            {
                                i.ProjectId,
                                i.TeamId,
                                i.Name
                            };
                foreach (var q2 in query2)
                {
                    var query3 = from i in db.Projects
                                 where i.ProjectId == q2.ProjectId
                                 select new
                                 {
                                     i.Name
                                 };
                    var query4 = from i in db.UserTeams
                                 where i.TeamId == q2.TeamId
                                select new
                                {
                                    i.UserId
                                };
                    var listMember = new List<MemberBean>();
                    foreach (var q4 in query4)
                    {
                        var query5 = from i in db.Users
                                     where i.UserId == q4.UserId
                                     select new
                                     {
                                         i.UserId,
                                         i.Username,
                                         i.Name,
                                         i.LastnameD,
                                         i.LastnameM,
                                         i.MajorId,
                                         i.Email
                                     };
                        listMember.Add(new MemberBean
                        {
                            userId = query5.First().UserId,
                            username = query5.First().Username,
                            name = query5.First().Name,
                            lastnameD = query5.First().LastnameD.Trim(),
                            lastnameM = query5.First().LastnameM.Trim(),
                            major = query5.First().MajorId.Trim(),
                            email = query5.First().Email
                        }); 
                    }

                    list.Add(new TeamBean
                    {
                        teamId = q2.TeamId,
                        projectId = q2.ProjectId,
                        teamName = q2.Name.Trim(),
                        projectName = query3.First().Name.Trim(),
                        memberList = listMember
                    }); 
                }

               
            }

            return list.ToArray();
        }

        [WebMethod]
        public MemberBean[] GetTeamMembers(int teamId)
        {
            var listMember = new List<MemberBean>();
            var query = from i in db.UserTeams
                        where i.TeamId == teamId
                        select new
                        {
                            i.TeamId,
                            i.UserId
                        };

            foreach (var q in query)
            {
                        var query5 = from i in db.Users
                                     where i.UserId == q.UserId
                                     select new
                                     {
                                         i.UserId,
                                         i.Username,
                                         i.Name,
                                         i.LastnameD,
                                         i.LastnameM,
                                         i.MajorId,
                                         i.Email,
                                         i.LastIP
                                     };
                        listMember.Add(new MemberBean
                        {
                            userId = query5.First().UserId,
                            username = query5.First().Username,
                            name = query5.First().Name,
                            lastnameD = query5.First().LastnameD.Trim(),
                            lastnameM = query5.First().LastnameM.Trim(),
                            major = query5.First().MajorId.Trim(),
                            email = query5.First().Email,
                            lastIp = query5.First().LastIP
                        }); 
            }
            return listMember.ToArray();
        }
    }


    #region Nested type: TeamBean
    public class TeamBean
    {
        public int teamId { get; set; }
        public int projectId { get; set; }
        public String teamName { get; set; }
        public String projectName { get; set; }
        public List<MemberBean> memberList { get; set; }
    }
    #endregion

    #region Nested type: MemberBean
    public class MemberBean
    {
        public int userId { get; set; }
        public String username { get; set; }
        public String name { get; set; }
        public String lastnameD { get; set; }
        public String lastnameM { get; set; }
        public String major { get; set; }
        public String email { get; set; }
        public String lastIp { get; set; }
    }
    #endregion
}
